package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {
    public static final String USER_IN_SESSION = "USER_IN_SESSION";

    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    /**
     * 获取session对象
     */
    public static HttpSession getSession(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return attributes.getRequest().getSession();
    }

    public static void fail(){
        System.out.println("fail");
    }


    //往session存入登录用户
    public static void setUserInSession(Employee employee){
        getSession().setAttribute(USER_IN_SESSION,employee);
    }
    /**
     * 获取当前登录用户
     */
    public static Employee getCurrentUser(){

        //SpringMVC提供的工具,可以在任意地方获取到request对象,respose对象,session对象
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return (Employee)attrs.getRequest().getSession().getAttribute(USER_IN_SESSION);
    }


    //往session存入登录用户的权限信息
    public static void setExpressionInSession(List<String> permissions){
        getSession().setAttribute(EXPRESSION_IN_SESSION,permissions);
    }
    public static void happy(){
        System.out.println("happy");
    }


    //从session获取登录用户的权限信息
    public static List<String> getExpression(){
        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }
}
